import { defineStore } from "pinia";

export const breadcrumbStore = defineStore({
  id: "breadcrumb",
  state: () => ({
    BreadcrumbList: [
      {
        key: "17",
        path: "/index",
        label: "首页",
      },
    ],
    selectedKeys: ["17"],
    openKeys: [],
  }),
  actions: {
    // 添加tab
    AddBreadcrumb(data) {
      if (
        this.BreadcrumbList.findIndex((e) => e.path === data.path) === -1 &&
        data.path !== "/index"
      ) {
        this.BreadcrumbList.push(data);
        this.selectedKeys = [data.key];
      }
    },
    // 删除tab
    removeBreadcrumb(path) {
      const index = this.BreadcrumbList.findIndex((e) => e.path === path);
      this.BreadcrumbList.splice(index, 1);
    },
    addOpenKeys(data) {
      if (data.length === 0 || this.openKeys.length === 0) {
        this.openKeys = data;
        return;
      }
      const target = this.openKeys[0];
      const index = data.indexOf(target);
      if (index !== -1) {
        data.splice(index, 1);
      }
      this.openKeys = data;
    },
  },
  getters: {},
});
